cfde7e
@@ -17,8 +17,10 @@
 package org.apache.camel.language.groovy;
 
 import groovy.lang.Script;
+import org.apache.camel.Service;
 import org.apache.camel.support.LanguageSupport;
 import org.apache.camel.util.LRUSoftCache;
+import org.codehaus.groovy.runtime.InvokerHelper;
 
 /**
  * @version
@@ -26,7 +28,26 @@
import org.apache.camel.util.LRUSoftCache;
 public class GroovyLanguage extends LanguageSupport {
 
     // Cache used to stores the compiled scripts (aka their classes)
-    private final LRUSoftCache<String, Class<Script>> scriptCache = new LRUSoftCache<String, Class<Script>>(1000);
+    private final LRUSoftCache<String, GroovyClassService> scriptCache = new LRUSoftCache<String, GroovyClassService>(16, 1000, true);
+
+    private static final class GroovyClassService implements Service {
+
+        private final Class<Script> script;
+
+        private GroovyClassService(Class<Script> script) {
+            this.script = script;
+        }
+
+        @Override
+        public void start() throws Exception {
+        }
+
+        @Override
+        public void stop() throws Exception {
+            InvokerHelper.removeClass(script);
+        }
+
+    }
 
     public static GroovyExpression groovy(String expression) {
         return new GroovyLanguage().createExpression(expression);
@@ -42,11 +63,17 @@
public class GroovyLanguage extends LanguageSupport {
     }
 
     Class<Script> getScriptFromCache(String script) {
-        return scriptCache.get(script);
+        final GroovyClassService cached = scriptCache.get(script);
+
+        if (cached == null) {
+            return null;
+        }
+
+        return cached.script;
     }
 
     void addScriptToCache(String script, Class<Script> scriptClass) {
-        scriptCache.put(script, scriptClass);
+        scriptCache.put(script, new GroovyClassService(scriptClass));
     }
 
 }
